package com.dayatang.exam.domain;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * 填空题答�?
 * @author yang
 *
 */
@Entity
@DiscriminatorValue("FB")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class FillBlankSolution extends QuestionSolution {

	private static final long serialVersionUID = 2066758605065164654L;

	public FillBlankSolution() {
	}

	public FillBlankSolution(FillBlankQuestion question, int point, int position) {
		super(question, point, position);
	}

	@Override
	public FillBlankQuestion getQuestion() {
		return getRepository().get(FillBlankQuestion.class, getQuestion().getId());
	}

	@Override
	public void setSolution(String solution) {
		super.setSolution(solution);
		calculateScore();
	}

	public int[] getPointMap() {
		int maxPoint = getPoint() + 1;
		int[] result = new int[maxPoint];
		for (int i = 0; i < maxPoint; i++) {
			result[i] = i;
		}
		return result;
	}

	/**
	 * 计算得分
	 */
	public void calculateScore() {
		if (getSolution() == null) {
			return;
		}
		String[] stardardSolutions = getQuestion().getSolution().split(FillBlankQuestion.STANDARD_ANSWER_SEPERATOR);
		for (String standardSolution : stardardSolutions) {
			if (standardSolution.equalsIgnoreCase(getSolution())) {
				setScore(getPoint());
				return;
			}
		}
		setScore(0);
	}
}
